package com.bill.controller;

import com.bill.pojo.Bills;
import com.bill.service.BillsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/bills")
public class BillsController {

    @Resource
    BillsService billsService;

    @RequestMapping("/index")
    public String index(Model model,
                        @RequestParam(value = "type_id", required = false) String type_id1,
                        @RequestParam(value = "startTime", required = false) String startTime,
                        @RequestParam(value = "endTime", required = false) String endTime) {
        int type_id = 0;
        if (type_id1 != null) {
            type_id = Integer.valueOf(type_id1);
        }
        List<Bills> billsList = billsService.queryTypeTimeBills(type_id, startTime, endTime);
//        model.addAttribute("bills",billsService.findBillsAll());
        model.addAttribute("bills", billsList);
        model.addAttribute("bill_type", billsService.findBill_typeAll());
        return "index";
    }

    @RequestMapping("/toAdd")
    public String toAdd(@ModelAttribute Bills bills) {
        return "add";
    }

    @RequestMapping("/add")
    public String add(@ModelAttribute Bills bills) {
        System.out.println(bills);
        int result = billsService.insertBills(bills);
        if (result > 0) {
            System.out.println("添加成功！");
            return "redirect:index";
        } else {
            System.out.println("添加失败！");
            return "redirect:msg";
        }
    }

    @RequestMapping("/msg")
    @ResponseBody
    public String msg() {
        return "欢迎来到万恶的404和500页面请认真审查你的代码";
    }


}
